var express = require('express');
var router = express.Router();
var UserToken = require('../db/user_tokens.js');
var signDao = require('../dao/sign');
var SignRouter = module.exports;

/**
 * 用户签到得积分
 * @param  token
 * @param  app_sid
 *         url//sign/signIn
 */
SignRouter.signIn = function(req, res) {
    var par = req.body;
    if(!par.token) return res.json(new ERR('请先登录！', 403));
    if(!par.app_sid) return res.json(new ERR('app_sid不为空！', 400));
    UserToken.checkUserLogin(par.token, function(err, errCode, userInfo) {
        if(err) return res.json(new ERR(err, errCode));
        par.uid = userInfo.uid;
        par.account = userInfo.account;
        signDao.signIn(par, function(e, d) {
            if(e) {
                res.json(new ERR(e, 400))
            } else {
                var info = {};
                info.msg = '签到成功！';
                info.variable_score = d.variable_score;
                info.totalScore = parseInt(d.variable_score) + parseInt(d.sign_score);
                res.json(new PKG(info));
            }
        });
    });
}
router.post('/signIn', SignRouter.signIn);

/**
 * 领取首次积分礼包
 * @param  token
 * @param  app_sid
 *         url//sign/receiveScore
 */
SignRouter.receiveScore = function(req, res) {
    var par = req.body;
    if(!par.token) return res.json(new ERR('请先登录！', 403));
    if(!par.app_sid) return res.json(new ERR('app_sid不为空！', 400));
    UserToken.checkUserLogin(par.token, function(err, errCode, userInfo) {
        if(err) return res.json(new ERR(err, errCode));
        par.uid = userInfo.uid;
        par.account = userInfo.account;
        signDao.receiveScore(par, function(e) {
            if(e) {
                res.json(new ERR(e, 400))
            } else {
                res.json(new PKG('领取成功！'));
            }
        });
    });
}
router.post('/receiveScore', SignRouter.receiveScore);

/**
 * 查看自己的积分变动记录      （id  1  为查看签到记录）
 * @param  token
 * @param  app_sid
 * @param  activity_id    （score_activity 表的id）   usuz  1   (可不传  不传就是查看所有的积分变动)
 * @param  page
 * @param  size
 *         url//sign/logList
 */
SignRouter.logList = function(req, res) {
    var par = req.body;
    if(!par.token) return res.json(new ERR('请先登录！', 403));
    if(!par.app_sid) return res.json(new ERR('app_sid不为空！', 400));
    // if(!par.activity_id) return res.json(new ERR('activity_id不为空！', 400));
    if(!par.size) par.size = 10;
    par.size = parseInt(par.size);
    if(!par.page) {
        par.start = 0;
    } else {
        par.start = (parseInt(par.page) - 1) * parseInt(par.size);
    }
    UserToken.checkUserLogin(par.token, function(err, errCode, userInfo) {
        if(err) return res.json(new ERR(err, errCode));
        par.uid = userInfo.uid;
        par.account = userInfo.account;
        signDao.logList(par, function(e, data) {
            if(e) {
                res.json(new ERR(e, 400))
            } else {
                res.json(new PKG(data));
            }
        });
    });
}
router.post('/logList', SignRouter.logList);


/**
 * 获取总积分 和  是否连续签到
 * @param  token
 * @param  app_sid
 *         url//sign/getSignInfo
 */
SignRouter.getSignInfo = function(req, res) {
    var par = req.body;
    if(!par.token) return res.json(new ERR('请先登录！', 403));
    if(!par.app_sid) return res.json(new ERR('app_sid不为空！', 400));
    UserToken.checkUserLogin(par.token, function(err, errCode, userInfo) {
        if(err) return res.json(new ERR(err, errCode));
        par.uid = userInfo.uid;
        par.account = userInfo.account;
        signDao.getSignInfo(par, function(e, data) {
            if(e) {
                res.json(new ERR(e, 400))
            } else {
                res.json(new PKG(data));
            }
        });
    });
}
router.post('/getSignInfo', SignRouter.getSignInfo);

/**
 * 查看是否能领取新人积分礼包
 * @param  token
 * @param  app_sid
 *         url//sign/canReceive
 */
SignRouter.canReceive = function(req, res) {
    var par = req.body;
    if(!par.token) return res.json(new ERR('请先登录！', 403));
    if(!par.app_sid) return res.json(new ERR('app_sid不为空！', 400));
    UserToken.checkUserLogin(par.token, function(err, errCode, userInfo) {
        if(err) return res.json(new ERR(err, errCode));
        par.uid = userInfo.uid;
        par.account = userInfo.account;
        signDao.canReceive(par, function(e, data) {
            if(e) {
                res.json(new ERR(e, 400))
            } else {
                res.json(new PKG(data));
            }
        });
    });
}
router.post('/canReceive', SignRouter.canReceive);

module.exports = router;